package controller;

import dao.UserDAOImpl;
import model.TUser;
import utils.DispatcherUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/login.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userid = request.getParameter("userid");
        String pwd = request.getParameter("pwd");
        UserDAOImpl userDAO = new UserDAOImpl();
        TUser tUser = new TUser();
        if(userid==null||userid.equals("")){
            DispatcherUtils.openErrWeb("用户名不能为空","login.jsp",request,response);
            return;
        }
        if(pwd==null||pwd.equals("")){
            DispatcherUtils.openErrWeb("密码不能为空","login.jsp",request,response);
            return;
        }
        tUser = userDAO.login(userid,pwd);

        HttpSession session = request.getSession();
        session.setAttribute("loginuser",tUser);
        response.sendRedirect("index.do");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
